﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;

public class CampareMD5ToGenerateVersionNum
{
    public static void Execute(UnityEditor.BuildTarget target)
    {
        string platform = AssetBundleController.GetPlatformName(target);
        Execute(platform);
        AssetDatabase.Refresh();
    }

    // 对比对应版本目录下的VersionMD5和VersionMD5-old，得到最新的版本号文件VersionNum.xml
    public static void Execute(string platform)
    {
        string path = Path.Combine(Application.dataPath, "AssetBundlePackage/AssetBundle/" + platform);
        // 读取新旧MD5列表
        string newVersionMD5 = path + "/VersionNum/VersionMD5.xml";
        string oldVersionMD5 = path + "/VersionNum/VersionMD5-old.xml";
        Dictionary<string, string> dicDisInfo = CompareTwoXML(newVersionMD5, oldVersionMD5);
        if (dicDisInfo != null)
        {
            SaveFile(dicDisInfo, path + "/VersionNum/VersionDis.xml");
            AssetDatabase.Refresh();
            EditorUtility.DisplayDialog("", "Complete", "OK");
        }
        else
            Debug.LogError("没有改变的文件资源");
    }

    public static Dictionary<string, string> CompareTwoXML(string newFile, string oldFile)
    {
        if (string.Compare(GX.MD5.ComputeHashStringFromFile(newFile), GX.MD5.ComputeHashStringFromFile(oldFile)) == 0) //相等
        {
            return null;
        }
        else
        {
            //返回两个文件的path和md5值
            Dictionary<string, string> dicNewMD5Info = ReadMD5File(newFile);
            Dictionary<string, string> dicOldMD5Info = ReadMD5File(oldFile);
            Dictionary<string, string> dicDisInfo = new Dictionary<string, string>();

            foreach (KeyValuePair<string, string> newPair in dicNewMD5Info)
            {
                // 旧版本中有,还要对比md5值
                if (dicOldMD5Info.ContainsKey(newPair.Key))
                {
                    if (dicOldMD5Info[newPair.Key] != dicNewMD5Info[newPair.Key])
                    {
                        dicDisInfo.Add(newPair.Key, newPair.Value);
                    }
                }
                else
                {
                    dicDisInfo.Add(newPair.Key, newPair.Value);
                }
            }
            if (dicDisInfo.Count > 0)
                return dicDisInfo;
            else
                return null;
        }
    }

    static Dictionary<string, string> ReadMD5File(string fileName)
    {
        Dictionary<string, string> DicMD5 = new Dictionary<string, string>();

        // 如果文件不存在，则直接返回
        if (System.IO.File.Exists(fileName) == false)
            return DicMD5;

        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(fileName);
        XmlElement XmlRoot = XmlDoc.DocumentElement;

        foreach (XmlNode node in XmlRoot.ChildNodes)
        {
            if ((node is XmlElement) == false)
                continue;

            string file = (node as XmlElement).GetAttribute("path");
            string md5 = (node as XmlElement).GetAttribute("hash");
            if (DicMD5.ContainsKey(file) == false)
            {
                DicMD5.Add(file, md5);
            }
        }

        XmlRoot = null;
        XmlDoc = null;
        return DicMD5;
    }

    static void SaveFile(Dictionary<string, string> data, string savePath)
    {
        XmlDocument XmlDoc = new XmlDocument();
        XmlElement XmlRoot = XmlDoc.CreateElement("ResourcesAdd");
        XmlDoc.AppendChild(XmlRoot);

        foreach (KeyValuePair<string, string> pair in data)
        {
            XmlElement xmlElem = XmlDoc.CreateElement("File");
            XmlRoot.AppendChild(xmlElem);
            xmlElem.SetAttribute("path", pair.Key);
            xmlElem.SetAttribute("hash", pair.Value);
        }
        XmlDoc.Save(savePath);
        XmlRoot = null;
        XmlDoc = null;
    }
}